package com.server.http.handler.impl;

import java.util.HashMap;
import java.util.Map;

import com.server.background.data.Account;
import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.EncryptionTools;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月20日 下午2:00:23
*/
public class LoginAccount extends HttpHandler{
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		int status = 0;
		String failMsg = null;
		String token = "YL";
		long tokentime = System.currentTimeMillis();
		Account account = new Account();
		Map<String,Object> data = new HashMap<String,Object>();
		String uname = request.getParameter("uname");

		String pw = request.getParameter("pw");
		if(uname == "" || pw == "") {
			failMsg = "参数错误或参数值为空！";
		}else {
			try {
				account = DataBaseManager.GetInstance().GetAccountData(uname, EncryptionTools.MD5(pw));
				if(account.accountId == 0) {
					failMsg = "帐号或密码错误！";
				}else {
					status = 1;
				}
			} catch (Exception e) {
				e.printStackTrace();
				failMsg = e.getMessage();
			}
		}
		if(status == 1) {
			data.put("uid", account.accountId);
			data.put("uname", uname);
			data.put("power", account.power);
			data.put("app_power", account.app_power);
			data.put("token", token);
			data.put("tokentime", tokentime);
			SetReturnData(response,data,HttpProtocol.LOGINACCOUNT,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.LOGINACCOUNT,HttpReturnStatus.Failed);
		}
	}

}